package com.dextrys.testing.seleniumdemo.jxl.impl;

import java.io.File;
import java.io.IOException;

import com.dextrys.testing.seleniumdemo.jxl.IExcelAccess;
import com.dextrys.testing.seleniumdemo.util.Config;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public class ExcelAccessImpl implements IExcelAccess {

	@Override
	public Workbook getWorkBook(String filePath) {
		Workbook workbook = null;
		try {
			workbook = Workbook.getWorkbook(new File(filePath));
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return workbook;
	}

	@Override
	public Cell[] getCellsByName(String name) {
		Workbook wb = new ExcelAccessImpl().getWorkBook(Config.getObjectLibrary());
		Sheet sheet = wb.getSheet(0);
		for (int m = 0; m < sheet.getRows(); m++) {
			if (sheet.getCell(0, m).getContents().equals(name)) {
				return sheet.getRow(m);
			}
		}
		return null;
	}

	@Override
	public boolean generateReport(String caseName) {
		WritableWorkbook wwb = null;
		try {
			wwb = Workbook.createWorkbook(new File(Config.getTestReport()));
			wwb.createSheet("result", 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public void writeExc(File filename) {
		WritableWorkbook wwb = null;
		try {
			wwb = Workbook.createWorkbook(filename);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 创建Excel工作表
		WritableSheet ws = wwb.createSheet("通讯录", 0);// 创建sheet
		try {
			ws.mergeCells(0, 0, 2, 1);// 合并单元格(左列，左行，右列，右行)从第1行第1列到第2行第3列
			Label header = new Label(0, 0, "通讯录(191026班)");
			ws.addCell(header);// 写入头
			Label l = new Label(0, 2, "姓名");// 第3行
			ws.addCell(l);
			l = new Label(1, 2, "电话");
			ws.addCell(l);
			l = new Label(2, 2, "地址");
			ws.addCell(l);
			l = new Label(0, 3, "小祝");// 第4行
			ws.addCell(l);
			l = new Label(1, 3, "1314***0974");
			ws.addCell(l);
			l = new Label(2, 3, "武汉武昌");
			ws.addCell(l);
			l = new Label(0, 4, "小施");// 第5行
			ws.addCell(l);
			l = new Label(1, 4, "1347***5057");
			ws.addCell(l);
			l = new Label(2, 4, "武汉武昌");
			ws.addCell(l);
			ws.setColumnView(0, 20);// 设置列宽
			ws.setColumnView(1, 20);
			ws.setColumnView(2, 40);
			ws.setRowView(0, 400);// 设置行高
			ws.setRowView(1, 400);
			ws.setRowView(2, 500);
			ws.setRowView(3, 500);
			ws.setRowView(4, 500);
		} catch (RowsExceededException e1) {
			e1.printStackTrace();
		} catch (WriteException e1) {
			e1.printStackTrace();
		}

		// 输出流
		try {
			wwb.write();
		} catch (IOException ex) {
			// TODO 自动生成 catch 块
			ex.printStackTrace();
		}
		// 关闭流
		try {
			wwb.close();
		} catch (WriteException ex) {
			// TODO 自动生成 catch 块
			ex.printStackTrace();
		} catch (IOException ex) {
			// TODO 自动生成 catch 块
			ex.printStackTrace();
		}
		// outStream.close();
		System.out.println("写入成功！\n");
	}

	public static void main(String[] args) {
		new ExcelAccessImpl().writeExc(new File("d:\\demo.xls"));
	}

}